import numpy as np
import csv
import random
from datetime import datetime
from time import strftime


def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = np.array([])
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey =np.append(sKey, temp)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def multi_unique(inputList):
    str_list = np.array([])
    for item in inputList:
        temp_str = ""
        for x in item:
            temp_str += ";" + str(x)
        str_list = np.append(str_list, temp_str)
    #print str_list
    uni_str = np.unique(str_list)
    uni_float = np.array([])
    #print uni_str
    for item in uni_str:
        temp_item = item[1:]
        #print temp_item
        #break
        temp_list = temp_item.split(";")
        for temp_list_item in temp_list:
            uni_float = np.append(uni_float, float(temp_list_item))
    iLenInputList = inputList.shape
    uni_float.shape = (-1, iLenInputList[1])
    return uni_float
        

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()
    
    unitCSV = "C:/Documents and Settings/wang322/My Documents/Downloads/WelFlows_3_coid_output.csv"
    dataMatrix = build_data_list(unitCSV)
    #coorCSV = "C:/Documents and Settings/wang322/My Documents/Downloads/WelFlows_2_unique_co.csv"
    #coordinate = build_data_list(coorCSV)
    #print dataMatrix[0]
    iLen = dataMatrix.shape
    output = np.array([])

    uni_flow = multi_unique(dataMatrix[:,1:3])
    for i_flow in uni_flow:
        temp_data = np.array([])
        for i_data in dataMatrix:
            if (i_flow[0] == i_data[1]) & (i_flow[1] == i_data[2]):
                temp_data = np.append(temp_data, i_data)
        temp_data.shape = (-1, iLen[1])
        iLenTemp = temp_data.shape
        output = np.append(output, [i_flow[0], i_flow[1], iLenTemp[0]])
        for i in range(3,7):
            temp_value = temp_data[:,i]
            temp = [np.max(temp_value), np.min(temp_value), np.mean(temp_value), np.max(temp_value) - np.min(temp_value)]
            output = np.append(output, temp)
    output.shape = (-1, 19)
    #for item_o in oid:
        #for item_d in did:
            #for item in dataMatrix:
                #if (item[1] == item_o) & (item[2] == item_d):
                    #coordinate = np.append(coordinate, [item_o, item_d])
                    #break

    #coordinate.shape = (-1, 2)
    
    filePath = unitCSV[:-4] + "_output_FlowCount.csv"
    np.savetxt(filePath, output, delimiter=',')

    print "end at " + getCurTime()
    print "========================================================================"  

           
